
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.System;
import com.cmc.dao.SystemDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class SystemDaoHibernate extends BaseDaoHibernate implements SystemDao {

    /**
     * @see com.cmc.dao.SystemDao#getSystems(com.cmc.model.System)
     */
    public List getSystems(final System system) {
        return getHibernateTemplate().find("from System");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.
        if (system == null) {
            return getHibernateTemplate().find("from System");
        } else {
            // filter on properties set in the system
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(system).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(System.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }*/
    }

    /**
     * @see com.cmc.dao.SystemDao#getSystem(Long id)
     */
    public System getSystem(final Long id) {
        System system = (System) getHibernateTemplate().get(System.class, id);
        if (system == null) {
            log.warn("uh oh, system with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(System.class, id);
        }

        return system;
    }

    /**
     * @see com.cmc.dao.SystemDao#saveSystem(System system)
     */    
    public void saveSystem(final System system) {
        getHibernateTemplate().saveOrUpdate(system);
    }

    /**
     * @see com.cmc.dao.SystemDao#removeSystem(Long id)
     */
    public void removeSystem(final Long id) {
        getHibernateTemplate().delete(getSystem(id));
    }
}
